package org.ilead.iserve.service.impl;

import java.util.List;

import org.ilead.iserve.constants.MessageStateConstant;
import org.ilead.iserve.dao.IPrivateMessageDAO;
import org.ilead.iserve.model.PrivateMessage;
import org.ilead.iserve.service.IPrivateMessageService;

public class PrivateMessageService implements IPrivateMessageService {
	
	private IPrivateMessageDAO privateMessageDAO;
	
	public void setPrivateMessageDAO(IPrivateMessageDAO privateMessageDAO) {
		this.privateMessageDAO = privateMessageDAO;
	}

	@Override
	public void addMessage(PrivateMessage message) {
		privateMessageDAO.save(message);
	}

	@Override
	public void updateMessage(PrivateMessage message) {
		privateMessageDAO.merge(message);
	}

	@Override
	public void deleteMessage(PrivateMessage message) {
		privateMessageDAO.delete(message);
	}

	@Override
	public List<PrivateMessage> findByUserIdAndState(int userId, int state) {
		PrivateMessage message = new PrivateMessage();
		message.setTarget(userId);
		message.setReState(state);
		return privateMessageDAO.findByExample(message);
	}

	@Override
	public int findUnReadCountByUserId(int userId) {
		return findByUserIdAndState(userId, MessageStateConstant.INIT).size();
	}

	@Override
	public void deleteMessageById(int messageId) {
		privateMessageDAO.deleteById(messageId);
	}

	@Override
	public PrivateMessage findById(int messageId) {
		return privateMessageDAO.findById(messageId);
	}

}
